
clear

* please make sure that the current directory is the folder "dofiles": ".../dofiles"
* cd "/Users/rufei/Documents/Economics/MyProjects/RationedFertility/final_files/ReplicationPackage/dofiles"
pwd

use ../raw/census1982/census1982.dta, clear

rename serial hhcode

tab cn82a_province,m

clonevar prov=cn82a_province
bys prov: g n=_n
count if n==1 // 29 provinces
drop n

order hhcode prov

*** individual level information

* relation to the household head
clonevar hstatus = cn82a_relate
label list cn82a_relate_lbl

tab hstatus, nol

* age
tab cn82a_age,m nol

clonevar age=cn82a_age
replace age = . if age == 998

g int birthy = 1982 - age

* gender
tab cn82a_sex,m nolabel

g male=cn82a_sex==1
replace male=. if cn82a_sex==.

	* family status: father, mother or children
gen fstatus = 1 if (hstatus == 1 | hstatus == 2) & male == 1
replace fstatus = 2 if (hstatus == 1 | hstatus == 2) & male == 0
replace fstatus = 3 if hstatus == 3
replace fstatus = 4 if hstatus == 4
replace fstatus = 5 if hstatus == 5
replace fstatus = 6 if hstatus == 6
replace fstatus = 7 if hstatus == 7
replace fstatus = 8 if hstatus == 8
replace fstatus = 9 if missing(fstatus) & missing(hstatus)
tab fstatus, m 

label define fstatus_lbl 1 "father" 2 "mother" 3 "child" 4 "grandchild" 5 "grandparent" 6 "grandgrandparent" 7 "other relatives" 8 "other non-relatives"
label values fstatus fstatus_lbl

tab fstatus,m
	

* ethnic indicator
tab cn82a_ethnic,m nolabel

g han=cn82a_ethnic==1
replace han=. if cn82a_ethnic > 56 // foreinger, unidentified, or other errors

clonevar ethn = cn82a_ethnic

* no hukou information in census1982

* migrant status
tab cn82a_regstatu,m nol

clonevar migstatus = cn82a_regstatu

g localhukou = migstatus == 1
replace localhukou = . if missing(migstatus)

* education years
tab cn82a_educ, m 
tab cn82a_educ, m nolabel

label list cn82a_educ_lbl

clonevar edu = cn82a_educ

g eduy = 0 if cn82a_educ == 6
replace eduy = 6 if cn82a_educ == 5
replace eduy = 9 if cn82a_educ == 4
replace eduy = 12 if cn82a_educ == 3
replace eduy = 14 if cn82a_educ == 2
replace eduy = 16 if cn82a_educ == 1

tab eduy, m


* Labor force participation 

tab cn82a_industry,m
tab cn82a_occ,m
tab cn82a_nonwork,m
gen emp = 1 if cn82a_industry ~=0
replace emp = 1 if cn82a_occ ~=0
replace emp = 0 if cn82a_nonwork ~=0 

tab emp,m

clonevar industry = cn82a_industry
clonevar occupation = cn82a_occ
clonevar unempstatus = cn82a_nonwork

* agricultural employment
tostring industry, g(industry_str)
replace industry_str="" if industry_str=="0"
replace industry_str="0"+industry_str if length(industry_str)==2

count if length(industry_str) == 1
count if length(industry_str) == 2
count if length(industry_str) == 3

list industry_str in 1/100

gen agriemp = 0
replace agriemp = 1 if substr(industry_str,1,2)=="01"|substr(industry_str,1,2)=="02"|substr(industry_str,1,2)=="03"|substr(industry_str,1,2)=="04"
replace agriemp = . if missing(industry_str)

tab agriemp,m
drop industry_str



* fertility

tab cn82a_chborn,m nolabel

foreach i in cn82a_chborn cn82a_chsurv {

replace `i'=0 if `i'==99

}

clonevar chborn = cn82a_chborn
clonevar chsurv = cn82a_chsurv

tab1 chborn chsurv if male == 0

sum  chborn chsurv if male == 0 & (age > 64 | age < 15) & !missing(age)

foreach var of varlist chborn chsurv  {
replace `var' = . if male == 1
replace `var' = . if male == 0 & (age >= 65 | age < 15) & !missing(age)
}

compare chborn chsurv // some mortality
g chdece = chborn - chsurv

* recent birth
tab cn82a_birthord,m
label list cn82a_birthord_lbl
clonevar chbornLY = cn82a_birthord
replace chbornLY = . if cn82a_birthord == 9



* labels
label var age "Age"
label var birthy "Birth year"
label var male" Male"
label var han "Han"
label var edu "Education level"
label var eduy "Educational years"
label var agriemp "Agricultural employment"

label var chborn "# births"
label var chsurv "# survival children"
label var chdece "# deceased children"

	** save individual level data
drop cntry-cn82a_birthord
compress
	
save ../data/census1982individual, replace





